/**
 * 
 */
package com.oauth.auth.controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * @author asanthan
 * 
 */
@Controller
public class LogOutController {

	@Value("${auth.login.url}")
	private String mLoginUrl;

	@RequestMapping("/auth/logout")
	public void handleRequest(HttpServletRequest pRequest, HttpServletResponse pResponse)
			throws ServletException, IOException {
		UserService userService = UserServiceFactory.getUserService();

		if (userService == null || userService.getCurrentUser() == null) {
			pResponse.sendRedirect(mLoginUrl);
		} else {
			String googleLogOutUrl = userService.createLogoutURL(mLoginUrl);
			pResponse.sendRedirect(googleLogOutUrl);
		}
	}

	/**
	 * @param pLoginUrl
	 *            the loginUrl to set
	 */
	public void setLoginUrl(String pLoginUrl) {
		mLoginUrl = pLoginUrl;
	}

}